home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj9205.zip / 1005038A < prev    next >
Text File  |  1992-06-02  |  4KB  |  157 lines

  1.                    /* Listing 1 */
  2.  
  3. /*****************************************************
  4.            File Name: MATRIX_T.C
  5.        Expanded Name: matrix type
  6. Global Funciton List: cr_matrix
  7.                       cr_matrix_s
  8.                       fr_matrix
  9.                       fr_matrix_s
  10.          Description: Library of functions for
  11.                       creation and destruction of
  12.                       objects of type matrix_t.
  13. *****************************************************/
  14.  
  15. /* Standard C */
  16. #include <stdlib.h>
  17.  
  18. /* Own */
  19. #include <matrix_t.h>
  20.  
  21. /*****************************************************
  22.        Name: cr_matrix
  23.  Parameters: NumRow - number of rows in matrix 
  24.              NumCol - number of cols in matrix
  25.      Return: matrix_t type or NULL if out of memory
  26. Description: Constructor function for type matrix_t.
  27.              Creates a matrix of size NumRow by
  28.              NumCol on the heap.  The matrix resides
  29.              in multiple blocks of memory.
  30. *****************************************************/
  31. matrix_t cr_matrix( size_t NumRow, size_t NumCol )
  32.    {
  33.  
  34.    matrix_t Matrix;
  35.  
  36.    /* Allocate pointers to rows. */
  37.    Matrix = calloc( 1, NumRow * sizeof ( double * ) );
  38.  
  39.    if ( Matrix != NULL )
  40.       {
  41.  
  42.       size_t i;
  43.  
  44.       /* Allocate rows and set pointers to them. */
  45.       for ( i = 0; i < NumRow; i++ )
  46.          {
  47.  
  48.          Matrix[i] = calloc( NumCol,
  49.                sizeof ( double ) );
  50.  
  51.          if ( Matrix[i] == NULL )
  52.             {
  53.             /* Out of memory */
  54.             fr_matrix( Matrix, NumRow );
  55.             Matrix = NULL;
  56.             break;
  57.             }   /* if Matrix[i] */
  58.  
  59.          }   /* for i */
  60.       }   /* if Matrix */
  61.  
  62.    return ( Matrix );
  63.  
  64.    }   /* function cr_matrix */
  65.  
  66.  
  67. /*****************************************************
  68.        Name: cr_matrix_s
  69.  Parameters: NumRow - number of rows in matrix 
  70.              NumCol - number of cols in matrix
  71.      Return: matrix_t type or NULL is out of memory
  72. Description: Constructor function for type matrix_t.
  73.              Creates a matrix of size NumRow by
  74.              NumCol on the heap.  The matrix resides
  75.              in a single contiguous block of memory.
  76. *****************************************************/
  77. matrix_t cr_matrix_s( size_t NumRow, size_t NumCol )
  78.    {
  79.  
  80.    matrix_t Matrix;
  81.  
  82.    size_t SizeCol;
  83.  
  84.    SizeCol = NumCol * sizeof ( double );
  85.    Matrix = calloc( 1, NumRow * ( sizeof ( double * )
  86.          + SizeCol ) );
  87.  
  88.    if ( Matrix != NULL )
  89.       {
  90.       size_t i;
  91.       Matrix[0] = (double *)&( Matrix[NumRow] );
  92.       for ( i = 1; i < NumRow; i++ )
  93.          {
  94.          Matrix[i] = (double *)
  95.                &( Matrix[i - 1][NumCol] );
  96.          }
  97.       }
  98.  
  99.    return ( Matrix );
  100.  
  101.    }   /* function cr_matrix_s */
  102.  
  103.  
  104. /*****************************************************
  105.        Name: fr_matrix
  106.  Parameters: Matrix - matrix to free
  107.              NumRow - number of rows in matrix 
  108.              NumCol - number of cols in matrix
  109. Description: Destructor function for type matrix_t.
  110.              Frees a matrix that resides in multiple
  111.              blocks of memory and was created with the
  112.              function cr_matrix.
  113. *****************************************************/
  114. void fr_matrix( matrix_t Matrix, size_t NumRow )
  115.    {
  116.  
  117.    if ( Matrix != NULL )
  118.       {
  119.  
  120.       size_t i;
  121.  
  122.       for ( i = 0; i < NumRow; i++ )
  123.          {
  124.          if ( Matrix[i] != NULL )
  125.             {
  126.             free( Matrix[i] );
  127.             }
  128.          }   /* for i */
  129.  
  130.       free( Matrix );
  131.  
  132.       }   /* if Matrix */
  133.  
  134.    }   /* function fr_matrix */
  135.  
  136.  
  137. /*****************************************************
  138.        Name: fr_matrix_s
  139.  Parameters: Matrix - matrix to free
  140. Description: Destructor function for type matrix_t.
  141.              Frees a matrix that resides in a single
  142.              contiguous block of memory and was
  143.              created with the function cr_matrix_s.
  144. *****************************************************/
  145. void fr_matrix_s( matrix_t Matrix )
  146.    {
  147.  
  148.    if ( Matrix != NULL )
  149.       {
  150.       free( Matrix );
  151.       }   /* if Matrix */
  152.  
  153.    }   /* function fr_matrix_s */
  154.  
  155. /* End of File */
  156.  
  157.